package com.faithbj.danaliverpool.web.controller.admin;

import com.faithbj.common.configuration.Pager;
import com.faithbj.common.model.Region;
import com.faithbj.common.model.Roles;
import com.faithbj.common.utils.StringUtil;
import com.faithbj.danaliverpool.service.RoleService;
import com.faithbj.common.service.RegionService;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jrn E-mail: rinanjin@gmail.com
 * @version 0.1 
 * 创建时间：2012-6-8 下午11:34:31
 * 类说明
 */
@Controller
@RequestMapping("/faith/role")
public class AdminRoleController extends BaseAdminController
{
	private static final long serialVersionUID = 1L;

	@Resource
	private RoleService roleService;
	
	@Resource
	private RegionService regionService;
	
	private final String LIST = "/admin/role_list";
	
	private final String INPUT = "/admin/role_input";
	
	private final String OPERRATE_RETURN_URL = "/faith/role/list";
	
	@RequestMapping(value = "/list")
	public String list(@ModelAttribute("pager") Pager pager, ModelMap map)
	{
		if(pager.getPropertyList() == null && pager.getCompareTypeList() == null && pager.getKeywordList() == null)
		{
			pager = new Pager();
			List<String> propertyList = new ArrayList<String>();
			propertyList.add("isSystem");
			pager.setPropertyList(propertyList);
			List<String> compareTypeList = new ArrayList<String>();
			compareTypeList.add("eq");
			pager.setCompareTypeList(compareTypeList);
			List<Object> keywordList = new ArrayList<Object>();
			keywordList.add(false);
			pager.setKeywordList(keywordList);
		}
		else
		{
			pager.getPropertyList().add("isSystem");
			pager.getCompareTypeList().add("eq");
			if(pager.getKeywordList().size() == 0)
			{
				pager.getKeywordList().add("");
				pager.getKeywordList().add(false);
			}
			else
			{
				pager.getKeywordList().add(false);
			}
		}
		Pager pagerVal = roleService.findByPager(pager);
		map.put("pager", pagerVal);
		return LIST;
	}
	
	@RequestMapping(value = "add")
	public String add(ModelMap map)
	{
		List<Region> regionList = regionService.getAll();
		map.put("regionList", regionList);
		map.put("isAddAction", true);
		
		return INPUT;
	}
	
	@RequestMapping(value = "/save")
	public String save(@Valid Roles roles, Errors errors, ModelMap map, HttpServletRequest request)
	{
		if(roleService.get("name", roles.getName()) != null)
		{
			List<Region> regionList = regionService.getAll();
			
			map.put("error", "已存在同名角色");
			map.put("role", roles);
			map.put("regionList", regionList);
			map.put("regionId", request.getParameter("regionId"));
			map.put("isAddAction", true);
			return INPUT;
		}
		
		if(errors.hasErrors())
		{
			List<Region> regionList = regionService.getAll();
			
			map.put("regionList", regionList);
			map.put("isAddAction", true);
			return INPUT;
		}
		String regionId = request.getParameter("regionId");
		
		if(regionId != null)
		{
			Region region = regionService.get(regionId);
			if(region != null)
			{
				roles.setValue("ROLE_" + region.getCode() + "_" + StringUtil.getFirstLetter(region.getName()).toUpperCase());
			}
		}
		else
		{
			return SUCCESS;
		}
		roles.setIsSystem(false);
		roles.setOperator(getLoginUser().getUsername());
		roleService.save(roles);
		roleService.flush();
		map.put("redirectUrl", OPERRATE_RETURN_URL);
		return SUCCESS;
	}
	
	@RequestMapping(value = "/delete")
	public String delete(String id, ModelMap map)
	{
		roleService.delete(id);
		roleService.flush();
		map.put("redirectUrl", OPERRATE_RETURN_URL);
		return SUCCESS;
	}
}
